// pages/check/administration/add/add.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		addText: '选择分类',
		tap: 0,
		list: [],
		man: '',
		name: '',
		inventory: '',
		freight: 0,
		monkey: '',
		monkey1: '',
		share: '',
		contents: '',
		id: '',
		http: getApp().globalData.map
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log(options.id)
		wx.getStorage({
			key: 'uid',
			success: (e) => {
				this.setData({
					uid: e.data.uid
				})
			}
		})
		if (options.id != undefined) {
			wx.request({
				url: getApp().globalData.http1 + 'action=showGoods&gid=' + options.id,
				success: (e) => {
					let a = []
					e.data.data.albums.forEach(function(item, index) {
						a.push(item.original_path)
					})
					this.setData({
						id: options.id,
						data: e.data.data,
						list: a,
						addText: e.data.data.ClassName,
						brand: e.data.data.CLASS_ID,
						freight: e.data.data.weight,
						tap: e.data.data.is_sale,
						man: e.data.data.azFree,
						name: e.data.data.goods_name,
						inventory: e.data.data.kucun,
						monkey: e.data.data.price,
						monkey1: e.data.data.gy_price,
						share: e.data.data.yh_price,
						contents: e.data.data.content
					})
					console.log(this.data.list)
				}
			})
		}
	},
	tap(e) {
		this.setData({
			tap: e.detail.value
		})
	},
	sc() {
		const a = this.data.list
		if (a.length < 9) {
			let nan=9-a.length
			wx.showToast({
				title: '加载中',
				icon: 'loading'
			});
			let that = this
			wx.chooseImage({
				count: nan,
				sizeType: 'compressed',
				success: (e) => {
					if (this.data.id == '') {
						e.tempFilePaths.forEach(function(item, index) {
							a.push(item)
						})
					} else {
						e.tempFilePaths.forEach(function(item, index) {
							wx.uploadFile({
								url: getApp().globalData.http3,
								filePath: item,
								name: 'picturePath',
								success: function(res) {
									let c = JSON.parse(res.data)
									a.push(c.data.headImage)
									console.log(a)
								}
							})
						})
					}
					console.log(a)
					setTimeout(function(){
						that.setData({
							list: a
						})
					},300)
				}
			})
		}
	},
	dete(e) {
		let a = this.data.list
		a.splice(e.currentTarget.dataset.id, 1)
		this.setData({
			list: a
		})
	},
	name(e) {
		this.setData({
			name: e.detail.value
		})
	},
	inventory(e) {
		this.setData({
			inventory: e.detail.value
		})
	},
	freight(e) {
		this.setData({
			freight: e.detail.value
		})
	},
	monkey(e) {
		this.setData({
			monkey: e.detail.value
		})
	},
	monkey1(e) {
		this.setData({
			monkey1: e.detail.value
		})
	},
	share(e) {
		this.setData({
			share: e.detail.value
		})
	},
	man(e) {
		this.setData({
			man: e.detail.value
		})
	},
	sure() {
		let a = []
		console.log(a)
		let that = this
		if (this.data.list != [] && that.data.name != '' && that.data.inventory != '' && that.data.brand != '' &&
			that.data.monkey != '' && that.data.share != '' && that.data.monkey1 != '') {
			var promise = new Promise(function(resolve, reject) {
				if (that.data.id != '') {
					a = that.data.list
				}
				that.data.list.forEach(function(item, index) {
					console.log(item)
					wx.uploadFile({
						url: getApp().globalData.http3,
						filePath: item,
						name: 'picturePath',
						success: function(res) {
							let c = JSON.parse(res.data)
							a.push(c.data.headImage)
							console.log(a)
						}
					})
				})
				resolve();
			}).then(function(resolve, reject) {
				let b = a.join()
				console.log(b)
				let zuangtai = ''
				if (that.data.data != undefined) {
					zuangtai = 'action=editGoods&gid=' + that.data.id
				} else {
					zuangtai = 'action=addGoods'
				}
				wx.request({
					url: getApp().globalData.http1 + zuangtai + '&uid=' + that.data.uid + '&pic=' + b + '&goodsname=' + that.data
						.name + '&kucun=' +
						that.data.inventory + '&classID=' + that.data.brand + '&yunfei=' + that.data.freight + '&price=' + that.data
						.monkey +
						'&yhprice=' +
						that.data.share + '&gyprice=' + that.data.monkey1 + '&contents=' + that.data.contents + '&azfree=' + that.data
						.man +
						'&is_sale=' +
						that.data.tap,
					success: (e) => {
						console.log(e)
						if (e.data.code == 0) {
							wx.showToast({
								icon: 'none',
								title: e.data.msg
							})
						} else {
							wx.navigateBack({
								delta: 1
							})
						}
					}
				})
			})
		} else {
			wx.showToast({
				icon: 'none',
				title: '请正确填写内容'
			})
		}
	}
})
